package org.jeecg.modules.xm.service.impl;

import org.jeecg.modules.xm.entity.XmMain01;
import org.jeecg.modules.xm.entity.XmItem02;
import org.jeecg.modules.xm.entity.XmItem01;
import org.jeecg.modules.xm.mapper.XmItem02Mapper;
import org.jeecg.modules.xm.mapper.XmItem01Mapper;
import org.jeecg.modules.xm.mapper.XmMain01Mapper;
import org.jeecg.modules.xm.service.IXmMain01Service;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: xm_main_01
 * @Author: zhaodui
 * @Date:   2020-03-14
 * @Version: V1.0
 */
@Service
public class XmMain01ServiceImpl extends ServiceImpl<XmMain01Mapper, XmMain01> implements IXmMain01Service {

	@Autowired
	private XmMain01Mapper xmMain01Mapper;
	@Autowired
	private XmItem02Mapper xmItem02Mapper;
	@Autowired
	private XmItem01Mapper xmItem01Mapper;
	
	@Override
	@Transactional
	public void delMain(String id) {
		xmItem02Mapper.deleteByMainId(id);
		xmItem01Mapper.deleteByMainId(id);
		xmMain01Mapper.deleteById(id);
	}
	@Override
	@Transactional
	public void updateMain(XmMain01 xmMain01,List<XmItem02> xmItem02List,List<XmItem01> xmItem01List) {
		xmMain01Mapper.updateById(xmMain01);

		//1.先删除子表数据
		xmItem02Mapper.deleteByMainId(xmMain01.getId());
		xmItem01Mapper.deleteByMainId(xmMain01.getId());

		//2.子表数据重新插入
		if(xmItem02List!=null && xmItem02List.size()>0) {
			for(XmItem02 entity:xmItem02List) {
				//外键设置
				entity.setQuery40(xmMain01.getId());
				xmItem02Mapper.insert(entity);
			}
		}
		if(xmItem01List!=null && xmItem01List.size()>0) {
			for(XmItem01 entity:xmItem01List) {
				//外键设置
				entity.setQuery40(xmMain01.getId());
				xmItem01Mapper.insert(entity);
			}
		}
	}
	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			xmItem02Mapper.deleteByMainId(id.toString());
			xmItem01Mapper.deleteByMainId(id.toString());
			xmMain01Mapper.deleteById(id);
		}
	}
	
}
